﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BDSim
{
    public partial class SatelliteSpaceUC : UserControl
    {
        #region 变量定义
        public delegate void SatelliteInfoHandler(object obj, string handleType);
        public event SatelliteInfoHandler EventOnData_SatelliteInfo;

        public List<SatPredictStatus> GPSSatStatusList = new List<SatPredictStatus>();
        public List<SatPredictStatus> BD2SatStatusList = new List<SatPredictStatus>();
        public List<SatPredictStatus> GLOSatStatusList = new List<SatPredictStatus>();
        public List<SatPredictStatus> GALSatStatusList = new List<SatPredictStatus>();
        private int GPSViewNum = 0; //GPS可见星数目
        private int BD2ViewNum = 0; //BD2可见星数目
        private int GLOViewNum = 0; //GLONASS可见星数目
        private int GALViewNum = 0; //GALILEO可见星数目

        //用于焦点显示
        private SatPredictStatus FocusSat = new SatPredictStatus();
        private bool IsFocus = false;
        private int SelectSys = -1;
        private int MouseX;
        private int MouseY;

        //DOP值信息
        public DOPValue DOP = new DOPValue();
        #endregion

        #region 构造函数
        public SatelliteSpaceUC()
        {
            InitializeComponent();
        }
        #endregion

        #region 绘制星空图
        private void PbSat_Paint(object sender, PaintEventArgs e)
        {
            //if (chk_BD2.Checked == true)
            //{
                Graphics g = e.Graphics;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

                #region 设置虚线
                Pen mydashpen = new Pen(Color.Black, 0.5f);
                Pen myWhitePen = new Pen(Color.White, 0.5f);
                mydashpen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
                mydashpen.DashPattern = new float[] { 0.5f, 2.0f };  //实体长和空格长
                myWhitePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
                myWhitePen.DashPattern = new float[] { 1f, 2.0f };  //实体长和空格长
                #endregion

                Font drawfont = new Font("Arial", 9, FontStyle.Bold);//字体
                Pen mypen = new Pen(Color.Black, 1);
                Pen mypen1 = new Pen(Color.White, 1);
                SolidBrush myBlackBrush = new SolidBrush(Color.Black);
                SolidBrush myWhiteBrush = new SolidBrush(Color.White);

                Color customColor = Color.FromArgb(0, 0, 0); //E1EAF5
                SolidBrush BackColor = new SolidBrush(customColor);

                int radius;                          //底图圆半径
                int radiusTemp;                      //用来画里面两个圈的临时半径
                Point cx = new Point();              //底图圆心坐标
                Point cy = new Point();              //卫星圆心半径
                Rectangle rect = new Rectangle();    //最外圈正方形大小和起点
                Rectangle rectTemp = new Rectangle();//用来画里面两个圈的正方形

                #region 初始化最外圈正方形大小和起点
                rect.X = 0;
                rect.Y = 0;
                rect.Height = PbSat.Height;
                rect.Width = PbSat.Width;
                if (PbSat.Width > PbSat.Height ) //25为星空图下方DOP值信息
                {
                    rect.Width = PbSat.Height - 20;
                    rect.Height = PbSat.Height - 20;
                    rect.Y = 10;
                    rect.X = (PbSat.Width - PbSat.Height ) / 2 + 10;
                }
                else if (PbSat.Width < PbSat.Height)
                {
                    rect.Width = PbSat.Width - 20;
                    rect.Height = PbSat.Width - 20;
                    rect.X = 10;
                    rect.Y = (PbSat.Height - PbSat.Width ) / 2 + 10;
                }
                else
                {
                    rect.Width = PbSat.Width - 20;
                    rect.Height = PbSat.Width - 20;
                    rect.Y = 10;
                    rect.X = 10;
                }
                #endregion

                #region 画方位角刻度的圆
                rect.X += 5;
                rect.Y += 5;
                rect.Width -= 10;
                rect.Height -= 10;
                radius = rect.Width / 2;
                g.DrawEllipse(mypen, rect);

                g.DrawString("N", drawfont, myBlackBrush, rect.Left + radius - 5, rect.Top - 18);
                g.DrawString("S", drawfont, myBlackBrush, rect.Left + radius - 5, rect.Bottom + 2);
                g.DrawString("W", drawfont, myBlackBrush, rect.Left - 15, rect.Top + radius - 8);
                g.DrawString("E", drawfont, myBlackBrush, rect.Right + 5, rect.Top + radius - 8);

                g.TranslateTransform(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2); //设置旋转的中心点
                for (int x = 0; x < 12; x++)
                {
                    g.RotateTransform(30);
                    g.FillRectangle(myBlackBrush, new Rectangle(-1, radius - 6, 2, 6));
                }
                #endregion

                #region 画纬度为0度的圆
                g.TranslateTransform(-rect.Left - rect.Width / 2, -rect.Top - rect.Height / 2);
                radius -= 9;
                rect.X += 9;
                rect.Y += 9;
                rect.Width -= 18;
                rect.Height -= 18;
                g.FillEllipse(BackColor, rect);
                g.DrawEllipse(myWhitePen, rect);
                Point drawpoint = new Point(rect.X + rect.Width / 2 - 9, rect.Y + 2);
                g.DrawString("0", drawfont, myWhiteBrush, drawpoint);


                #endregion

                #region 画纬度为30度的圆，半径为最外圆Cos(30*PI/180)
                radiusTemp = (int)(radius * Math.Cos(30 * Math.PI / 180)); // radiusTemp = radius / 3 * 2;
                rectTemp.X = rect.Left + (radius - radiusTemp);
                rectTemp.Y = rect.Top + (radius - radiusTemp);
                rectTemp.Width = radiusTemp * 2;
                rectTemp.Height = radiusTemp * 2;
                g.DrawEllipse(myWhitePen, rectTemp);
                drawpoint = new Point(rectTemp.X + rectTemp.Width / 2 - 14, rectTemp.Y + 2);
                g.DrawString("30", drawfont, myWhiteBrush, drawpoint);
                #endregion

                #region 画纬度为60度的圆，半径为最外圆Cos(60*PI/180)=1/2（非1/3）
                radiusTemp = radius / 2;//radiusTemp = radius / 3;
                rectTemp.X = rect.Left + (radius - radiusTemp);
                rectTemp.Y = rect.Top + (radius - radiusTemp);
                rectTemp.Width = radiusTemp * 2;
                rectTemp.Height = radiusTemp * 2;
                g.DrawEllipse(myWhitePen, rectTemp);
                drawpoint = new Point(rectTemp.X + rectTemp.Width / 2 - 14, rectTemp.Y + 2);
                g.DrawString("60", drawfont, myWhiteBrush, drawpoint);
                #endregion

                #region 画90度标识
                radiusTemp = 0;
                rectTemp.X = rect.Left + (radius - radiusTemp);
                rectTemp.Y = rect.Top + (radius - radiusTemp);
                rectTemp.Width = radiusTemp * 2;
                rectTemp.Height = radiusTemp * 2;
                drawpoint = new Point((rectTemp.X + rectTemp.Width / 2) - 14, rectTemp.Y + 2);
                g.DrawString("90", drawfont, myWhiteBrush, drawpoint);
                #endregion

                #region 画2条直径线
                cx.X = rect.Left + rect.Width / 2;
                cx.Y = rect.Top + rect.Height / 2 - radius;
                cy.X = rect.Left + rect.Width / 2;
                cy.Y = rect.Top + rect.Height / 2 + radius;
                g.DrawLine(myWhitePen, cx, cy);

                cx.X = rect.Left + rect.Width / 2 - radius;
                cx.Y = rect.Top + rect.Height / 2;
                cy.X = rect.Left + rect.Width / 2 + radius;
                cy.Y = rect.Top + rect.Height / 2;
                g.DrawLine(myWhitePen, cx, cy);
                #endregion

                #region 此处加入控制显示的代码
                cx.X = rect.Left + radius;
                cx.Y = rect.Top + radius;

                if (chk_BD2.Checked == true)
                {
                    myBlackBrush.Color = Color.FromArgb(100, Color.Red);
                    if (IsFocus && SelectSys == 1 && FocusSat.Visible == true)
                    {
                        g.FillEllipse(myBlackBrush, FocusSat.ScreenX - 15, FocusSat.ScreenY - 15, 30, 30);
                    }
                    BD2SatPlot(g, radius, myBlackBrush, cx, drawfont, rectTemp);
                }

                if (chk_GPS.Checked == true)
                {
                    myBlackBrush.Color = Color.FromArgb(100, Color.Red);
                    if (IsFocus && SelectSys == 0 && FocusSat.Visible == true)
                    {
                        g.FillEllipse(myBlackBrush, FocusSat.ScreenX - 15, FocusSat.ScreenY - 15, 30, 30);
                    }
                    GPSSatPlot(g, radius, myBlackBrush, cx, drawfont, rectTemp);
                }

                if (chk_GLONASS.Checked == true)
                {
                    myBlackBrush.Color = Color.FromArgb(100, Color.Red);
                    if (IsFocus && SelectSys == 2 && FocusSat.Visible == true)
                    {
                        g.FillEllipse(myBlackBrush, FocusSat.ScreenX - 15, FocusSat.ScreenY - 15, 30, 30);
                    }
                    GLONASSSatPlot(g, radius, myBlackBrush, cx, drawfont, rectTemp);
                }

                if (chk_GALILEO.Checked == true)
                {
                    myBlackBrush.Color = Color.FromArgb(100, Color.Red);
                    if (IsFocus && SelectSys == 3 && FocusSat.Visible == true)
                    {
                        g.FillEllipse(myBlackBrush, FocusSat.ScreenX - 15, FocusSat.ScreenY - 15, 30, 30);
                    }
                    GALILEOSatPlot(g, radius, myBlackBrush, cx, drawfont, rectTemp);
                }
                #endregion

                #region 画弹出框
                if (IsFocus && FocusSat.Visible == true)
                {
                    myBlackBrush.Color = Color.FromArgb(200, Color.WhiteSmoke);
                    drawfont = new Font("Arial", 8, FontStyle.Regular);
                    rect.X = MouseX + 10;
                    rect.Y = MouseY;
                    rect.Width = 155;
                    rect.Height = 162;

                    if (rect.X + rect.Width > PbSat.Width)
                    {
                        //rect.X -= rect.Width;
                        rect.X = rect.X - rect.Width - 20;
                    }
                    if (rect.Y + rect.Height > PbSat.Height)
                    {
                        //rect.Y -= rect.Height;
                        rect.Y = 10;
                    }
                    g.FillRectangle(myBlackBrush, rect);
                    g.DrawRectangle(mypen, rect);

                    myBlackBrush.Color = Color.FromArgb(200, Color.Navy);
                    double[] xyz = new double[] { FocusSat.PosX, FocusSat.PosY, FocusSat.PosZ };
                    double[] lbh = new double[3];
                    lbh = xyz2lbh(xyz);

                    string sysStr = string.Empty;
                    if (SelectSys == 0)
                    {
                        sysStr = "GPS ";
                    }
                    else if (SelectSys == 1)
                    {
                        sysStr = "BDS ";
                    }
                    else if (SelectSys == 2)
                    {
                        sysStr = "GLONASS ";
                    }
                    else if (SelectSys == 3)
                    {
                        sysStr = "GALILEO ";
                    }

                    //g.DrawString(sysStr + "SVN" + FocusSat.PRN.ToString(), drawfont, mybrush, rect.X + 2, rect.Y + 4);
                    g.DrawString("SVN:" + FocusSat.satName.ToString(), drawfont, myBlackBrush, rect.X + 2, rect.Y + 4);
                    g.DrawString("方位角：" + FocusSat.Azimuth.ToString("f1") + "°", drawfont, myBlackBrush, rect.X + 2, rect.Y + 20);
                    g.DrawString("俯仰角：" + FocusSat.Elevation.ToString("f1") + "°", drawfont, myBlackBrush, rect.X + 2, rect.Y + 32);
                    g.DrawString("PosX:  " + FocusSat.PosX.ToString("f3") + " m", drawfont, myBlackBrush, rect.X + 2, rect.Y + 48);
                    g.DrawString("PosY:  " + FocusSat.PosY.ToString("f3") + " m", drawfont, myBlackBrush, rect.X + 2, rect.Y + 60);
                    g.DrawString("PosZ:  " + FocusSat.PosZ.ToString("f3") + " m", drawfont, myBlackBrush, rect.X + 2, rect.Y + 72);
                    g.DrawString("经度：" + lbh[0].ToString("f4") + "°", drawfont, myBlackBrush, rect.X + 2, rect.Y + 88);
                    g.DrawString("纬度：" + lbh[1].ToString("f4") + "°", drawfont, myBlackBrush, rect.X + 2, rect.Y + 100);
                    g.DrawString("高程：" + lbh[2].ToString("f3") + " m", drawfont, myBlackBrush, rect.X + 2, rect.Y + 112);
                    g.DrawString("伪距:  " + FocusSat.PseudoRange.ToString("f3") + " m", drawfont, myBlackBrush, rect.X + 2, rect.Y + 128);
                    g.DrawString("载波相位:  " + FocusSat.Phase.ToString("f3") + " m", drawfont, myBlackBrush, rect.X + 2, rect.Y + 144);
                }
                #endregion

                #region 绘制图示标签
                drawfont = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
                GetVisibleNum();
                //label2.Text = BD2ViewNum.ToString();
                //BD2
                myBlackBrush.Color = Color.FromArgb(200, Color.Green);
                //rect.X = PbSat.Width - 75;
                rect.X = 10;
                rect.Y = 5;
                rect.Width = 20;
                rect.Height = 15;
                g.FillRectangle(myBlackBrush, rect);
                g.DrawRectangle(mypen, rect);

                cx.X = rect.X + rect.Width + 4;
                cx.Y = rect.Y;
                string str = "BDS:" + BD2ViewNum.ToString();
                myBlackBrush.Color = Color.FromArgb(200, Color.Purple);
                g.DrawString(str, drawfont, myBlackBrush, cx);

                //GPS
                myBlackBrush.Color = Color.FromArgb(200, Color.DeepSkyBlue);
                //rect.X = PbSat.Width - 75;
                rect.X = 10;
                rect.Y = 25;
                rect.Width = 20;
                rect.Height = 15;
                g.FillRectangle(myBlackBrush, rect);
                g.DrawRectangle(mypen, rect);

                cx.X = rect.X + rect.Width + 4;
                cx.Y = rect.Y;
                str = "GPS:" + GPSViewNum.ToString();
                myBlackBrush.Color = Color.FromArgb(200, Color.Purple);
                g.DrawString(str, drawfont, myBlackBrush, cx);

                //GLONASS
                myBlackBrush.Color = Color.FromArgb(200, Color.SlateBlue);
                //rect.X = PbSat.Width - 75;
                rect.X = 10;
                rect.Y = 45;
                rect.Width = 20;
                rect.Height = 15;
                g.FillRectangle(myBlackBrush, rect);
                g.DrawRectangle(mypen, rect);

                cx.X = rect.X + rect.Width + 4;
                cx.Y = rect.Y;
                str = "GLO:" + GLOViewNum.ToString();
                myBlackBrush.Color = Color.FromArgb(200, Color.Purple);
                g.DrawString(str, drawfont, myBlackBrush, cx);

                //GALILEO
                myBlackBrush.Color = Color.FromArgb(200, Color.Goldenrod);
                //rect.X = PbSat.Width - 75;
                rect.X = 10;
                rect.Y = 65;
                rect.Width = 20;
                rect.Height = 15;
                g.FillRectangle(myBlackBrush, rect);
                g.DrawRectangle(mypen, rect);

                cx.X = rect.X + rect.Width + 4;
                cx.Y = rect.Y;
                str = "GAL:" + GALViewNum.ToString();
                myBlackBrush.Color = Color.FromArgb(200, Color.Purple);
                g.DrawString(str, drawfont, myBlackBrush, cx);
                #endregion

                #region 绘制DOP值信息
                //drawfont = new System.Drawing.Font("Arial", 9, FontStyle.Bold);
                //mybrush.Color = Color.FromArgb(200, Color.Purple);
                //cx.X = 5;
                //cx.Y = PbSat.Height - 15;
                //if (DOP.GDOP <= 0 || DOP.HDOP <= 0 || DOP.PDOP <= 0 || DOP.VDOP <= 0 || DOP.TDOP <= 0)
                //{
                //    str = "GDOP: --" + "    PDOP: --" + "    HDOP: --" + "    VDOP: --" + "    TDOP: --";
                //}
                //else
                //{
                //    str = "GDOP: " + DOP.GDOP.ToString("f1") + "    PDOP: " + DOP.PDOP.ToString("f1") + "    HDOP: " + DOP.HDOP.ToString("f1") + "    VDOP: " + DOP.VDOP.ToString("f1") + "    TDOP: " + DOP.TDOP.ToString("f1");
                //}
                //g.DrawString(str, drawfont, mybrush, cx);
                #endregion

                mypen.Dispose();
                myBlackBrush.Dispose();
                mydashpen.Dispose();
                drawfont.Dispose();
            //}
        }
        #endregion

        #region 绘制GPS系统卫星信息
        private void GPSSatPlot(Graphics g, int R, SolidBrush brush, Point CenterOfSky, Font textFont, Rectangle rect)
        {
            for (int i = 0; i < GPSSatStatusList.Count; i++)
            {
                if (GPSSatStatusList[i].Visible == false)  //不绘制不可见星
                {
                    continue;
                }

                //根据卫星方位角(Azimuth)、俯仰角(Elevation)计算卫星在星空图中的坐标（x,y）
                double angle = GPSSatStatusList[i].Azimuth + 90;
                angle = (angle / 180.0) * Math.PI;  //转换为弧度
                double r = R * Math.Cos(GPSSatStatusList[i].Elevation * Math.PI / 180); //卫星位置距星空图中心的距离
                if (GPSSatStatusList[i].Elevation < 0) //不绘制俯仰角小于0度的卫星
                {
                    continue;
                }

                int x = Convert.ToInt32(Math.Round(CenterOfSky.X - (r * Math.Cos(angle)), 0, MidpointRounding.AwayFromZero));
                int y = Convert.ToInt32(Math.Round(CenterOfSky.Y - (r * Math.Sin(angle)), 0, MidpointRounding.AwayFromZero));
                GPSSatStatusList[i].ScreenX = x;
                GPSSatStatusList[i].ScreenY = y;

                //绘制卫星点（小圆）
                rect.X = x - 10;
                rect.Y = y - 10;
                rect.Width = 20;
                rect.Height = 20;
                brush.Color = Color.DeepSkyBlue;
                g.FillEllipse(brush, rect);

                //绘制星号
                Point pt = new Point(x - 8, y - 8);
                brush.Color = Color.White;
                string PRNStr = string.Empty;
                PRNStr = GPSSatStatusList[i].PRN.ToString("D2");
                g.DrawString(PRNStr, textFont, brush, pt);
            }
        }
        #endregion

        #region 绘制BD2系统卫星信息
        private void BD2SatPlot(Graphics g, int R, SolidBrush brush, Point CenterOfSky, Font textFont, Rectangle rect)
        {
            BD2ViewNum = 0;
            for (int i = 0; i < BD2SatStatusList.Count; i++)
            {
                if (BD2SatStatusList[i].Visible == false)  //不绘制不可见星
                {
                    continue;
                }

                //根据卫星方位角(Azimuth)、俯仰角(Elevation)计算卫星在星空图中的坐标（x,y）
                double angle = BD2SatStatusList[i].Azimuth + 90;
                angle = (angle / 180.0) * Math.PI;  //转换为弧度
                double r = R * Math.Cos(BD2SatStatusList[i].Elevation * Math.PI / 180); //卫星位置距星空图中心的距离
                if (BD2SatStatusList[i].Elevation < 0) //不绘制俯仰角小于0度的卫星
                {
                    continue;
                }
                BD2ViewNum++;
                int x = Convert.ToInt32(Math.Round(CenterOfSky.X - (r * Math.Cos(angle)), 0, MidpointRounding.AwayFromZero));
                int y = Convert.ToInt32(Math.Round(CenterOfSky.Y - (r * Math.Sin(angle)), 0, MidpointRounding.AwayFromZero));
                BD2SatStatusList[i].ScreenX = x;
                BD2SatStatusList[i].ScreenY = y;

                //绘制卫星点（小圆）
                rect.X = x - 10;
                rect.Y = y - 10;
                rect.Width = 20;
                rect.Height = 20;
                brush.Color = Color.Green;
                g.FillEllipse(brush, rect);

                //绘制星号
                Point pt = new Point(x - 8, y - 8);
                brush.Color = Color.White;
                string PRNStr = string.Empty;
                PRNStr = BD2SatStatusList[i].PRN.ToString("D2");
                g.DrawString(PRNStr, textFont, brush, pt);
            }
        }
        #endregion

        #region 绘制GLONASS系统卫星信息
        private void GLONASSSatPlot(Graphics g, int R, SolidBrush brush, Point CenterOfSky, Font textFont, Rectangle rect)
        {
            for (int i = 0; i < GLOSatStatusList.Count; i++)
            {
                if (GLOSatStatusList[i].Visible == false)  //不绘制不可见星
                {
                    continue;
                }

                //根据卫星方位角(Azimuth)、俯仰角(Elevation)计算卫星在星空图中的坐标（x,y）
                double angle = GLOSatStatusList[i].Azimuth + 90;
                angle = (angle / 180.0) * Math.PI;  //转换为弧度
                double r = R * Math.Cos(GLOSatStatusList[i].Elevation * Math.PI / 180); //卫星位置距星空图中心的距离
                if (GLOSatStatusList[i].Elevation < 0) //不绘制俯仰角小于0度的卫星
                {
                    continue;
                }

                int x = Convert.ToInt32(Math.Round(CenterOfSky.X - (r * Math.Cos(angle)), 0, MidpointRounding.AwayFromZero));
                int y = Convert.ToInt32(Math.Round(CenterOfSky.Y - (r * Math.Sin(angle)), 0, MidpointRounding.AwayFromZero));
                GLOSatStatusList[i].ScreenX = x;
                GLOSatStatusList[i].ScreenY = y;

                //绘制卫星点（小圆）
                rect.X = x - 10;
                rect.Y = y - 10;
                rect.Width = 20;
                rect.Height = 20;
                brush.Color = Color.SlateBlue;
                g.FillEllipse(brush, rect);

                //绘制星号
                Point pt = new Point(x - 8, y - 8);
                brush.Color = Color.White;
                string PRNStr = string.Empty;
                PRNStr = GLOSatStatusList[i].PRN.ToString("D2");
                g.DrawString(PRNStr, textFont, brush, pt);
            }
        }
        #endregion

        #region 绘制GALILEO系统卫星信息
        private void GALILEOSatPlot(Graphics g, int R, SolidBrush brush, Point CenterOfSky, Font textFont, Rectangle rect)
        {
            for (int i = 0; i < GALSatStatusList.Count; i++)
            {
                if (GALSatStatusList[i].Visible == false)  //不绘制不可见星
                {
                    continue;
                }

                //根据卫星方位角(Azimuth)、俯仰角(Elevation)计算卫星在星空图中的坐标（x,y）
                double angle = GALSatStatusList[i].Azimuth + 90;
                angle = (angle / 180.0) * Math.PI;  //转换为弧度
                double r = R * Math.Cos(GALSatStatusList[i].Elevation * Math.PI / 180); //卫星位置距星空图中心的距离
                if (GALSatStatusList[i].Elevation < 0) //不绘制俯仰角小于0度的卫星
                {
                    continue;
                }

                int x = Convert.ToInt32(Math.Round(CenterOfSky.X - (r * Math.Cos(angle)), 0, MidpointRounding.AwayFromZero));
                int y = Convert.ToInt32(Math.Round(CenterOfSky.Y - (r * Math.Sin(angle)), 0, MidpointRounding.AwayFromZero));
                GALSatStatusList[i].ScreenX = x;
                GALSatStatusList[i].ScreenY = y;

                //绘制卫星点（小圆）
                rect.X = x - 10;
                rect.Y = y - 10;
                rect.Width = 20;
                rect.Height = 20;
                brush.Color = Color.Goldenrod;
                g.FillEllipse(brush, rect);

                //绘制星号
                Point pt = new Point(x - 8, y - 8);
                brush.Color = Color.White;
                string PRNStr = string.Empty;
                PRNStr = GALSatStatusList[i].PRN.ToString("D2");
                g.DrawString(PRNStr, textFont, brush, pt);
            }
        }
        #endregion

        #region 可见卫星数目统计
        private void GetVisibleNum()
        {
            GPSViewNum = 0;
            BD2ViewNum = 0;
            GLOViewNum = 0;
            GALViewNum = 0;

            if (chk_GPS.Checked == true)
            {
                for (int i = 0; i < GPSSatStatusList.Count; i++)
                {
                    if (GPSSatStatusList[i].Visible == true)
                    {
                        GPSViewNum++;
                    }
                }
            }
            else
            {
                GPSViewNum = 0;
            }

            if (chk_BD2.Checked == true)
            {
                for (int i = 0; i < BD2SatStatusList.Count; i++)
                {
                    if (BD2SatStatusList[i].Visible == true)
                    {
                        BD2ViewNum++;
                    }
                }
            }
            else
            {
                BD2ViewNum = 0;
            }

            if (chk_GLONASS.Checked == true)
            {
                for (int i = 0; i < GLOSatStatusList.Count; i++)
                {
                    if (GLOSatStatusList[i].Visible == true)
                    {
                        GLOViewNum++;
                    }
                }
            }
            else
            {
                GLOViewNum = 0;
            }

            if (chk_GALILEO.Checked == true)
            {
                for (int i = 0; i < GALSatStatusList.Count; i++)
                {
                    if (GALSatStatusList[i].Visible == true)
                    {
                        GALViewNum++;
                    }
                }
            }
            else
            {
                GALViewNum = 0;
            }
        }
        #endregion

        #region 窗体大小改变事件
        private void SatelliteInfoFrm_Resize(object sender, EventArgs e)
        {
            PbSat.Invalidate();
        }
        #endregion

        #region 鼠标移近卫星事件
        private void PbSat_MouseMove(object sender, MouseEventArgs e)
        {
            float len = 1000000.0f;

            if (chk_GALILEO.Checked == true)
            {
                foreach (SatPredictStatus viewdata in GALSatStatusList)
                {
                    float temp = (float)(Math.Pow(viewdata.ScreenX - e.X, 2) + Math.Pow(viewdata.ScreenY - e.Y, 2));
                    if (temp < len)
                    {
                        len = temp;
                        FocusSat = viewdata;
                        SelectSys = 3;
                    }
                }
            }

            if (chk_GLONASS.Checked == true)
            {
                foreach (SatPredictStatus viewdata in GLOSatStatusList)
                {
                    float temp = (float)(Math.Pow(viewdata.ScreenX - e.X, 2) + Math.Pow(viewdata.ScreenY - e.Y, 2));
                    if (temp < len)
                    {
                        len = temp;
                        FocusSat = viewdata;
                        SelectSys = 2;
                    }
                }
            }

            if (chk_GPS.Checked == true)
            {
                foreach (SatPredictStatus viewdata in GPSSatStatusList)
                {
                    float temp = (float)(Math.Pow(viewdata.ScreenX - e.X, 2) + Math.Pow(viewdata.ScreenY - e.Y, 2));
                    if (temp < len)
                    {
                        len = temp;
                        FocusSat = viewdata;
                        SelectSys = 0;
                    }
                }
            }

            if (chk_BD2.Checked == true)
            {
                foreach (SatPredictStatus viewdata in BD2SatStatusList)
                {
                    float temp = (float)(Math.Pow(viewdata.ScreenX - e.X, 2) + Math.Pow(viewdata.ScreenY - e.Y, 2));
                    if (temp < len)
                    {
                        len = temp;
                        FocusSat = viewdata;
                        SelectSys = 1;
                    }
                }
            }

            if (len < 55)
            {
                IsFocus = true;
            }
            else
            {
                IsFocus = false;
            }

            MouseX = e.X;
            MouseY = e.Y;
            PbSat.Invalidate();
        }
        #endregion

        #region 根据模拟源频点信息显示相应的卫星系统
        private void HandleSystemAccordingToFreq(List<UserToFreqInfo> freqLists)
        {
            //chk_BD2.Checked = false;
            //chk_BD2.Enabled = false;
            //chk_GPS.Checked = false;
            //chk_GPS.Enabled = false;
            //chk_GLONASS.Checked = false;
            //chk_GLONASS.Enabled = false;
            //chk_GALILEO.Checked = false;
            //chk_GALILEO.Enabled = false;

            for (int i = 0; i < freqLists.Count; i++)
            {
                if (freqLists[i].SystemSat == "BDS")
                {
                    //chk_BD2.Checked = true;
                    //chk_BD2.Enabled = true;
                }
                //else if (freqLists[i].SystemSat == "GPS")
                //{
                //    chk_GPS.Checked = true;
                //    chk_GPS.Enabled = true;
                //}
                //else if (freqLists[i].SystemSat == "GLO")
                //{
                //    chk_GLONASS.Checked = true;
                //    chk_GLONASS.Enabled = true;
                //}
                //else if (freqLists[i].SystemSat == "GAL")
                //{
                //    chk_GALILEO.Checked = true;
                //    chk_GALILEO.Enabled = true;
                //}
            }
        }
        #endregion

        #region 窗体关闭之前事件
        private void SatelliteInfoFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (EventOnData_SatelliteInfo != null)
            {
                EventOnData_SatelliteInfo("", "Close");
            }
        }
        #endregion

        /// <summary>
        /// ECF转LBH
        /// </summary>
        /// <param name="xyz"></param>
        /// <returns></returns>
        double[] xyz2lbh(double[] xyz)
        {
            double[] lbh = { 0, 0, 0 };
            double Alpha_E = 0.335281066475e-2;//1./298.257;//
            double a_E = 6378137.0;

            if (xyz[0] == 0)
                return lbh;
            lbh[0] = Math.Atan2(xyz[1], xyz[0]);

            short EndFlag = -1;
            double B = 0, N = 0;
            double B0 = 0;
            double e2 = 2 * Alpha_E - Alpha_E * Alpha_E;
            double d = Math.Sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1]);
            while (EndFlag < 0)
            {
                N = a_E / Math.Sqrt(1 - e2 * Math.Sin(B) * Math.Sin(B));
                B = Math.Atan2((xyz[2] + N * e2 * Math.Sin(B)), d);
                if (Math.Abs(B0 - B) < 1e-10)
                    EndFlag = 1;
                B0 = B;
                lbh[1] = B;
            }
            double d1 = xyz[2] + N * e2 * Math.Sin(B);
            d = d * d + d1 * d1;
            lbh[2] = Math.Sqrt(d) - N;

            lbh[0] = lbh[0] * 180 / Math.PI;
            lbh[1] = lbh[1] * 180 / Math.PI;

            return lbh;
        }

        public void InvokePaint()
        {
            PbSat.Invalidate();
        }

        public void ClearPaint()
        {
            BD2SatStatusList.Clear();
            GPSSatStatusList.Clear();
            GLOSatStatusList.Clear();
            GALSatStatusList.Clear();
            PbSat.Invalidate();
        }
    }
}
